define(function (require) {
    var $ = require("jquery");
    require('angular-ui');
    var util = require("util");
    var ui = require("ui");
    var _ = require('underscore');
    var angular = require("angular");
    require('jqtree');

    angular.module('salesMove', [
    ])
        .controller('MoveSalesController', [
            '$scope',
            '$rootScope',
            '$compile',
            function ($scope,$rootScope, $compile) {

                var $tree_old = $("#tree_table_old")
                    .tree()
                    .on('tree.select', function (e) {
                        if (e.node && e.node.data) {
                            var data = e.node.data;
                            if (data.length === 1) {
                                $scope.selected = $scope.teams[data[0]];
                            } else {
                                $scope.selected = $scope.teams[data[0]].groups[data[1]];
                            }
                            $scope.setOldOrgDisplay();
                            $scope.$apply();
                        }
                    });

                var $tree_new = $("#tree_table_new")
                    .tree()
                    .on('tree.select', function (e) {
                        if (e.node && e.node.data) {
                            var data = e.node.data;
                            if (data.length === 1) {
                                $scope.selectedOld = $scope.teams[data[0]];
                            } else {
                                $scope.selectedOld = $scope.teams[data[0]].groups[data[1]];
                            }
                            $scope.setNewOrgDisplay();
                            $scope.$apply();
                        }
                    });


                var teams = $scope.teams = $rootScope.salesTeamInfo['teams'];
                
                var teamList = [];
                for (var i = 0; i < teams.length; i++) {
                    var team = teams[i];
                    teamList[i] = {};
                    teamList[i].name = team.team_name;
                    teamList[i].data = [i];
                    
                    if (team.groups && team.groups.length && team.groups[0].group_name) {
                        teamList[i].children = [];
                        for (var j = 0; j < team.groups.length; j++) {
                            teamList[i].children.push({
                                name: team.groups[j].group_name,
                                data: [i,j]
                            });
                        }  
                    }
                               
                }
                
                $scope.teamList = teamList;
                
                $tree_old.tree('loadData',teamList);
                $tree_new.tree('loadData',teamList);               
                
                $compile($tree_old)($scope);
                $compile($tree_new)($scope);
                
                $scope.setOldOrgDisplay = function(){
                    $scope.showOldOrg = !$scope.showOldOrg;
                    $scope.oldOrgStyle = {
                        border:"none",
                        display: $scope.showOldOrg ? "block": "none"
                    };
                };
                
                $scope.setNewOrgDisplay = function(){
                    $scope.showNewOrg = !$scope.showNewOrg;
                    $scope.newOrgStyle = {
                        border:"none",
                        display: $scope.showNewOrg ? "block": "none"
                    };
                };
                
                $scope.memberList = [];
                $scope.selectMemberList = [];
                $scope.checker = {
                    selectItem: function (index) {
                        var item = $scope.memberList[index];
                        var checkedIndex = _.findIndex($scope.selectMemberList, {user_id:item.user_id});
                        if (item.checked) {
                            checkedIndex === -1 && $scope.selectMemberList.push(angular.copy(item));
                        }else if(!item.checked){

                            //如果全选按钮选中，则取消选中
                            $scope.checker.all && ($scope.checker.all = false);
                            checkedIndex !== -1 && $scope.selectMemberList.splice(checkedIndex,1);
                        }
                        $rootScope.selectMemberList = $scope.selectMemberList;
                    },
                    selectAll: function () {
                        var all = $scope.checker.all;
                        if(all){
                            _.each($scope.memberList,function(item,index){
                                item.checked = true;
                                $scope.checker.selectItem(index);
                            });
                        }else{
                            _.each($scope.memberList,function(item,index){
                                item.checked = false;
                                $scope.checker.selectItem(index);
                            });
                        }
                    }
                };

                $scope.downChecker = {
                    selectItem: function (index) {
                        var item = $scope.selectMemberList[index];
                        if (!item.checked) {
                            $scope.downChecker.all = false;
                        }
                    },
                    selectAll: function () {
                        var list = $scope.selectMemberList;
                        var all = true;
                        for (var i = 0; i < list.length; i++) {
                            var item = list[i];
                            if (!item.checked) {
                                all = false;
                                break;
                            }
                        }
                        for (var i = 0; i < list.length; i++) {
                            var item = list[i];
                            item.checked = !all;
                        }
                        $scope.downChecker.all = !all;
                    }
                };

                $scope.addItem = function () {
                    var list = $scope.memberList;
                    var chooseItem = false;
                    for (var i = 0; i < list.length; i++) {
                        var item = list[i];
                        if (item.checked) {
                            item.checked = false;
                            list.splice(i, 1);
                            i--;
                            var selected = false;
                            for (var j = 0; j < $scope.selectMemberList.length; j++) {
                                if ($scope.selectMemberList[j].user_id === item.user_id) {
                                    selected = true;
                                    break;
                                }
                            }
                            !selected && $scope.selectMemberList.push(angular.copy(item));
                            chooseItem = true;
                        }
                    }
                    if (!chooseItem) {
                        $rootScope.$emit('httpRequestError',"请选择操作的行");
                    }
                    $scope.checker.all = false;
                    $rootScope.selectMemberList = $scope.selectMemberList;
                };

                $scope.removeItem = function (index,item) {
                    var checkedItem = _.find($scope.memberList, {user_id:item.user_id});
                    checkedItem  && (checkedItem.checked = false);
                    $scope.selectMemberList.splice(index,1);
                };
                
                $scope.search = _.debounce(function () {
                    
                }, 250); 
                
                $scope.$watch('selected',function(selected){
                    if (selected) {
                        var memberList = [];
                        
                        if (selected.team_id) {
                            if (selected.groups && selected.groups.length) {
                                for (var i = 0; i < selected.groups.length; i++) {
                                    var group = selected.groups[i];
                                    if (group.salers && group.salers.length) {                                    
                                        memberList = memberList.concat(group.salers);
                                    }
                                    
                                    if (group.group_managers) {
                                        memberList = memberList.concat(group.group_managers);
                                    }                                  
                                }
                            }
                        }
                        if (selected.team_managers) {
                            memberList = memberList.concat(selected.team_managers);
                        }
                        if (selected.group_managers) {
                            memberList = memberList.concat(selected.group_managers);
                        }
                        if (selected.salers) {
                            memberList = memberList.concat(selected.salers);
                        }
                        // 初始化member.checked
                        _.each(memberList,function(member){
                            member.checked = false;
                        });
                        $scope.memberList = memberList;
                    }
                });
                
                $scope.$watch('selectedOld',function(selected){
                    if (selected) {
                        $rootScope.selected = selected;
                    }
                });
            }
        ])
});